package hongcaosp.app.android.video.online.holder;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.video.online.bean.OnlineMusic;
import xlj.lib.android.base.toastcompat.ToastManager;

public class OnlineMusicHolder extends BaseHolder<OnlineMusic> {

    private ImageView iv_cover,iv_status;
    private ProgressBar pb_prepare;
    private TextView tv_music_name,tv_music_author,tv_use;
    private IOnItemClickListener onItemClickListener;

    public OnlineMusicHolder(View itemView, IOnItemClickListener onItemClickListener) {
        super(itemView);
        this.onItemClickListener = onItemClickListener;
        iv_cover = itemView.findViewById(R.id.iv_cover);
        iv_status = itemView.findViewById(R.id.iv_status);
        pb_prepare = itemView.findViewById(R.id.pb_prepare);
        tv_music_name = itemView.findViewById(R.id.tv_music_name);
        tv_music_author = itemView.findViewById(R.id.tv_music_author);
        tv_use = itemView.findViewById(R.id.tv_use);

        tv_use.setOnClickListener(useClickListener);
        itemView.setOnClickListener(itemClickListener);
    }

    @Override
    public void setData(OnlineMusic onlineMusic) {
        super.setData(onlineMusic);
        Glide.with(iv_cover.getContext())
                .load(onlineMusic.getCoverImg())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.icon_user_head)
                .into(iv_cover);
        tv_music_name.setText(onlineMusic.getSongName());
        tv_music_author.setText(onlineMusic.getSingerName());

        tv_use.setVisibility(View.GONE);
        switch (onlineMusic.getStatus()) {
            case 0:
                pb_prepare.setVisibility(View.GONE);
                iv_status.setVisibility(View.VISIBLE);
                iv_status.setBackgroundResource(R.drawable.online_play);
                break;
            case 1:
                iv_status.setVisibility(View.GONE);
                pb_prepare.setVisibility(View.VISIBLE);
                break;
            case 2:
                pb_prepare.setVisibility(View.GONE);
                tv_use.setVisibility(View.VISIBLE);
                iv_status.setVisibility(View.VISIBLE);
                iv_status.setBackgroundResource(R.drawable.online_stop);
                break;
        }
    }

    private View.OnClickListener itemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = getData().getSongUrl();
            if (TextUtils.isEmpty(url)) {
                ToastManager.getInstance().showToast("音乐地址为空");
                return;
            }
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getData());
            }
        }
    };

    private View.OnClickListener useClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = getData().getSongUrl();
            if (TextUtils.isEmpty(url)) {
                ToastManager.getInstance().showToast("音乐地址为空");
                return;
            }
            if (onItemClickListener != null) {
                onItemClickListener.onUseClick(getData());
            }
        }
    };

    public interface IOnItemClickListener {
        void onItemClick(OnlineMusic music);

        void onUseClick(OnlineMusic music);
    }
}
